﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Windows;

namespace DataMining.DataAccess
{
    public class RowValueFromDataTable
    {
        /// <summary>
        /// 从bean中获取属性，并且把属性的值赋予row中相同字的段名
        /// </summary>
        /// <param name="obj">数据对象</param>
        /// <param name="dataRow">数据行</param>
        public static void GetValueFromTable(Object obj, DataRow dataRow)
        {
            if (obj == null)
            {
                throw new Exception("数据对象不能为空");
            }
            if (dataRow == null)
            {
                throw new Exception("数据行不能为空");
            }
            ///获得数据对象的所有的属性
            PropertyInfo[] properties = obj.GetType().GetProperties();
            try
            {
                foreach(PropertyInfo property in properties)
                {
                    try
                    {
                        Object returnValue = property.GetValue(obj, null);
                        dataRow[property.Name] = returnValue;
                    }
                    catch (Exception e)
                    {
                        //获取数据为空的异常
                        MessageBox.Show(property.Name+"   "+ e.Message);
                        
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }
    }
}
